<template>
  <el-dialog title="新增员工" :visible="showDialog" @close='btnCancel'>
    <!-- 表单 -->
    <el-form ref="addEmployee" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择" >
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber"  style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
           <!-- 焦点事件 -->
        <el-input @focus="getDepartments" v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" />
        <!-- 树形结构 -->

        <el-tree 
        v-loading="loading"
        @node-click="selectNode"    
        v-if="showTree" 
        :data="treeData" 
        :props="{ label:'name'}">
        </el-tree>
      </el-form-item>
      <el-form-item :default-expand-all="true" v-loading="loading" label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import  EmployeeEnum from '@/api/constant/employees'
import { tranListToTreeData } from '@/utils'
import { getDepartments } from '@/api/departments'
import { addEmployee } from "@/api/employees";
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      EmployeeEnum, // 在data中定义数据
      formData: {
        username: '',  //姓名
        mobile: '',   //手机号
        formOfEmployment: '',  //聘用形式
        workNumber: '',  //工号
        departmentName: '', //部门
        timeOfEntry: '',  //入职时间
        correctionTime: ''  //转正时间
      },
      rules:{
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 2, max: 10, message: '用户姓名为2-4位', trigger: 'blur'
        }],  //姓名
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],   //手机号
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],  //聘用形式
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],  //工号
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }], //部门
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }],  //入职时间
        correctionTime: ''  //转正时间
      },
      treeData: [], // 定义数组接收树形数据
      loading: false,
      showTree: false, // 控制树形的显示或者隐藏
    }
  },
  methods: {
    // 部门数据
    async getDepartments(){
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      //把depts转换成树形结构 然后用el-tree显示
      console.log(depts);
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    // 聘用形式
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    // 确认
    async btnOK(){
      try{
      //做手动校验
        await this.$refs.addEmployee.validate()
      // 调用新增接口
        await addEmployee(this.formData)
        // this.$parent 可以直接调用到父组件的实例 实际上就是父组件this
        this.$parent.getEmployeeList()
        this.$parent.showDialog = false
      }catch(error){
        console.log(error);
      }
    },
    //取消
    btnCancel(){
      // 重置原来的数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.addEmployee.resetFields() // 重置校验结果
      this.$emit('update:showDialog', false) // 关闭弹层 因为父传子的时候使用了.sync修饰符
    }
  }
}
</script>

<style>

</style>
